/**
Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>

This file is part of Vos Tutos Offline.

Vos Tutos Offline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vos Tutos Offline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mainwindow.h"
#include "alert.h"
#include "latex.h"
#include "zcode.h"

#include <QToolBar>
#include <QMessageBox>
#include <QFileDialog>
#include <QFile>
#include <QInputDialog>

void VTOMainWindow::initToolBar()
{
    QToolBar* newToolBar = new QToolBar(this);
    newToolBar->addAction(vtoMenuBar->newNewsItem);
    newToolBar->addAction(vtoMenuBar->newMiniTutorial);
    newToolBar->addAction(vtoMenuBar->newBigTutorial);

    QToolBar* fsBar = new QToolBar(this);
    fsBar->addAction(vtoMenuBar->open);
    fsBar->addAction(vtoMenuBar->save);
    fsBar->addAction(vtoMenuBar->saveAs);
    fsBar->addAction(vtoMenuBar->close);
    fsBar->addSeparator();
    fsBar->addAction(vtoMenuBar->exportZcode);
    fsBar->addAction(vtoMenuBar->exportLatex);
    fsBar->addAction(vtoMenuBar->exportHtml);


    QToolBar* tutoBar = new QToolBar(this);
    tutoBar->addAction(vtoMenuBar->newChapter);
    tutoBar->addAction(vtoMenuBar->newMinituto);
    tutoBar->addAction(vtoMenuBar->newPart);
    tutoBar->addSeparator();

    tutoBar->addAction(vtoMenuBar->up);
    tutoBar->addAction(vtoMenuBar->down);
    tutoBar->addAction(vtoMenuBar->remove);

    addToolBar(newToolBar);
    addToolBar(fsBar);
    addToolBar(tutoBar);
}



void VTOMainWindow::newNews()
{
    if(closeDocument())
	editionMode(new Part(tr("A News")), VTO::NEWS);
}

void VTOMainWindow::newMiniTutorial()
{
    if(closeDocument())
	editionMode(new MiniTutorial(tr("A Minitutorial")), VTO::MINITUTORIAL);
}

void VTOMainWindow::newBigTutorial()
{
    if(closeDocument())
	editionMode(new BigTutorial(tr("A Big Tutorial")), VTO::BIGTUTORIAL);
}
/* I/O Menu */

void VTOMainWindow::open()
{
    if(!closeDocument())
	return;
			
    QString fname = QFileDialog::getOpenFileName(this, tr("Open a file"),
						 QString(), tr("VTO Files (*.vto)"));

    if(fname.isEmpty())
	return;
    
    QDomDocument doc("file");
    QFile file(fname);

    try{
	if( file.open(QIODevice::ReadOnly) )
	{
	    if( doc.setContent( &file ) )
	    {
		file.close();
			
		QDomElement root = doc.documentElement();
			
		if(root.tagName() != "document")
		    throw 300;
		if(root.attribute("type") == "part")
		    editionMode(new Part(root.firstChildElement()), VTO::NEWS);
		else if(root.attribute("type") == "minituto")
		    editionMode(new MiniTutorial(root.firstChildElement()), VTO::MINITUTORIAL);
		else if(root.attribute("type") == "bigtuto")
		    editionMode(new BigTutorial(root.firstChildElement()), VTO::BIGTUTORIAL);
		else
		    throw 300;
				
		tutorial->setFilename(fname);
	    }
	    else
	    {
		file.close();
		throw 301;
	    }
	}
	else
	    throw 400;
    }
    catch(int e)
    {
	VTOAlert::alert(e, this);
    }

}

void VTOMainWindow::save()
{
    if(tutorial == NULL)
	return;

    QString fname = tutorial->getFilename();
    
    if(fname.isEmpty())
    {
	fname = QFileDialog::getSaveFileName(this,tr("Save the file"),QString(),
					     tr("VTO File (*.vto)"));
	if(fname.isEmpty())
	    return;
    }
    try{
	saveAs(fname);
    }
    catch(int e){
	VTOAlert::alert(e, this);
    }
}

void VTOMainWindow::saveAs(QString fname)
{
    if(tutorial == NULL)
	return;
	
    if(fname.isEmpty())
    {
	fname = QFileDialog::getSaveFileName(this,tr("Save the file"),QString(),
					     tr("VTO File (*.vto)"));
	if(fname.isEmpty())
	    return;
    }

    if(tutorialtype == VTO::NEWS)
    {
	Part* news = (Part*) tutorial;
	news->toFile(fname);
    }
    else if(tutorialtype == VTO::MINITUTORIAL)
    {
	MiniTutorial* minituto = (MiniTutorial*) tutorial;
	minituto->toFile(fname);
    }
    else if(tutorialtype == VTO::BIGTUTORIAL)
    {
	BigTutorial* bigtuto = (BigTutorial*) tutorial;
	bigtuto->toFile(fname);
    }
    else
    {
	throw 101;
    }
    
    setWindowModified(false);
    statusBar->showMessage(tr("Document saved to ")+fname, 5000);
}

void VTOMainWindow::exportLatex()
{
	if(tutorial == NULL)
	return;
	
    QString fname = QFileDialog::getSaveFileName(this,tr("Save the file"),QString(),
						 tr("LaTeX File (*.tex)"));
    if(fname.isEmpty())
	return;

    try{
	
	if(tutorialtype == VTO::NEWS)
	{
	    Part* news = (Part*) tutorial;
	    Latex::partToFile(news, fname);
	}
	else if(tutorialtype == VTO::MINITUTORIAL)
	{
	    MiniTutorial* minituto = (MiniTutorial*) tutorial;
	    Latex::miniToFile(minituto, fname);
	}
	else if(tutorialtype == VTO::BIGTUTORIAL)
	{
	/*
	    BigTutorial* bigtuto = (BigTutorial*) tutorial;
	    Zcode::bigToFile(bigtuto, fname);
	*/
	}
	else
	{
	    throw 101;
	}
    
    }catch(int e){
    	VTOAlert::alert(e);
    }
    
    statusBar->showMessage(tr("Document exported to ")+fname, 5000);

}

void VTOMainWindow::exportZcode()
{
    if(tutorial == NULL)
	return;
	
    QString fname = QFileDialog::getSaveFileName(this,tr("Save the file"),QString(),
						 tr("Tuto File (*.tuto)"));
    if(fname.isEmpty())
	return;

    try{
	
	if(tutorialtype == VTO::NEWS)
	{
	    Part* news = (Part*) tutorial;
	    Zcode::partToFile(news, fname);
	}
	else if(tutorialtype == VTO::MINITUTORIAL)
	{
	    MiniTutorial* minituto = (MiniTutorial*) tutorial;
	    Zcode::miniToFile(minituto, fname);
	}
	else if(tutorialtype == VTO::BIGTUTORIAL)
	{
	    BigTutorial* bigtuto = (BigTutorial*) tutorial;
	    Zcode::bigToFile(bigtuto, fname);
	}
	else
	{
	    throw 101;
	}
    
    }catch(int e){
    	VTOAlert::alert(e);
    }
    
    statusBar->showMessage(tr("Document exported to ")+fname, 5000);
}

/* Inside an existing doc */

void VTOMainWindow::newChapter()
{
    if(docSelectedType == VTO::BIGTUTORIAL)
    {
	QString title = QInputDialog::getText(
	    this,
	    tr("Enter the Chapter's title"),
	    tr("Please enter the title for the new chapter"));
	
	if(title != QString())
	{
	    BigTutorial* bt = (BigTutorial*) docSelected;
	    bt->allParts().append(
		new BigTutorialPart(title));

	    tutotree->refresh();
	}
    }
    else
    	VTOAlert::alert(102, this);
}

void VTOMainWindow::newMinituto()
{
    if(docSelectedType == VTO::CHAPTER)
    {
	QString title = QInputDialog::getText(
	    this,
	    tr("Enter the Minituto's title"),
	    tr("Please enter the title for the new minituto"));
	
	if(title != QString())
	{
	    BigTutorialPart* btp = (BigTutorialPart*) docSelected;
	    btp->allChapters().append(
		new MiniTutorial(title));

	    tutotree->refresh();
	}

    }
    else
    	VTOAlert::alert(103, this);   
}

void VTOMainWindow::newPart()
{
    if(docSelectedType == VTO::MINITUTORIAL)
    {
	QString title = QInputDialog::getText(
	    this,
	    tr("Enter the Part's title"),
	    tr("Please enter the title for the new Part"));
	
	if(title != QString())
	{
	    MiniTutorial* mt = (MiniTutorial*) docSelected;
	    mt->allParts().append(
		new Part(title));

	    tutotree->refresh();
	}
    }
    else
	VTOAlert::alert(104, this);
}


bool VTOMainWindow::warning_closeDocument()
{
    if(!isWindowModified())
	return true;
		
    if(tutorial != NULL)
    {
    	int res = VTOAlert::alert(120, this);
	if(res == QMessageBox::No)
	    return true;
	else if(res == QMessageBox::Yes)
	{
	    try{
		save();
	    }catch(int e){
		VTOAlert::alert(e, this);
		return false;
	    }
			
	    return true;
	}
	else
	    return false;
    }
    else
	return true;
}

bool VTOMainWindow::closeDocument()
{
    if(tutorial != NULL)
    {
	if(warning_closeDocument())
	{
	    delete tutorial;

	    tutorial = NULL;
	    tutorialtype = -1;
	    docSelected = NULL;
	    docSelectedType = -1;

	    clearLayout();
	    return true;
	}
	else
	    return false;
    }
    else
	return true;
}


void VTOMainWindow::closeEvent(QCloseEvent* event)
{
    if(closeDocument())
    {
	writeSettings();
	event->accept();
    }
    else
	event->ignore();
}
